package com.clp.util.hutool.core.util;

import com.clp.util.hutool.core.io.IORuntimeException;
import com.clp.util.hutool.core.net.URLDecoder;

import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.jar.JarFile;

public class URLUtil {
    public static String decode(String content, String charset) {
        return decode(content, CharsetUtil.charset(charset));
    }

    /**
     * 解码 application/x-www-form-urleocoded 字符 将%开头的16进制表示的内容解码
     * @param content
     * @param charset 为null表示不解码
     * @return
     */
    public static String decode(String content, Charset charset) {
        if (null == charset) {
            return content;
        }
        return URLDecoder.decode(content, charset);
    }

    public static JarFile getJarFile(URL url) {
        try {
            JarURLConnection urlConnection = (JarURLConnection) url.openConnection();
            return urlConnection.getJarFile();
        } catch (IOException e) {
            throw new IORuntimeException(e);
        }
    }
}
